﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharpDemo._05_数组
{
    class Person : IComparable<Person>
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }

        public override string ToString()
        {
            return string.Format("FistName:{0}, LastName:{1}", this.FirstName, this.LastName);
        }

        public Person(string FirstName, string LastName)
        {
            this.FirstName = FirstName;
            this.LastName = LastName;
        }

        public Person() { }

        //按照名排序
        public int CompareTo(Person Other)
        {
            return this.FirstName.CompareTo(Other.FirstName);
        }
    }

    class ArrayClass
    {
        public static void Test()
        {
            //1.创建数组(动态创建类的数组时很有用)
            Array PersonArray = Array.CreateInstance(typeof(Person), 2);
            PersonArray.SetValue(new Person("zhang", "san"), 0);
            PersonArray.SetValue(new Person("li", "si"), 1);

            //2.强转数组
            Person[] myPerson = (Person[])PersonArray;
            Console.WriteLine("\n数组方法测试");
            Console.WriteLine(myPerson[0].ToString());

            //3.克隆方法(浅拷贝,只是复制了引用)
            Person[] clonePerson = (Person[])myPerson.Clone();
            clonePerson[0].FirstName = "jack";
            Console.WriteLine(myPerson[0].ToString());

            //4.数组排序:需要类实现了IComparable接口。改变了需要排序的类
            Console.WriteLine("\n实现Icomparable接口按FirstName排序");
            Person[] sortPerson = {new Person { FirstName = "c" , LastName = "a"},
                                   new Person { FirstName = "b" , LastName = "b"},
                                   new Person { FirstName = "a" , LastName = "c"}};
            Array.Sort(sortPerson);
            for (int i = 0; i < sortPerson.Length; i++)
            {
                Console.WriteLine(sortPerson[i].ToString());
            }

            //5.使用比较类来排序（好处是不需要改变需要排序的类）
            Console.WriteLine("\n使用排序类按LastName排序");
            Array.Sort(sortPerson, new PersonCompare(PersonCompareType.LastName));
            for (int i = 0; i < sortPerson.Length; i++)
            {
                Console.WriteLine(sortPerson[i].ToString());
            }
        }
    }


    public enum PersonCompareType
    {
        FirstName,
        LastName
    }
    class PersonCompare : IComparer<Person>
    {
        //比较类型
        private PersonCompareType compareType;
        public PersonCompare(PersonCompareType compareType)
        {
            this.compareType = compareType;
        }

        //实现接口定义的比较方法
        public int Compare(Person x, Person y)
        {
            if (compareType == PersonCompareType.FirstName)
            {
                return string.Compare(x.FirstName, y.FirstName);
            }
            else if (compareType == PersonCompareType.LastName)
            {
                return string.Compare(x.LastName, y.LastName);
            }
            else {
                throw new ArgumentException("没有初始化比较类型");
            }
        }
    }
}
